package com.example.administrator.uitest.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import com.example.administrator.uitest.R;
import com.example.administrator.uitest.intface.DoView;
import com.example.administrator.uitest.request.Userlogin_Request;
import com.example.administrator.uitest.tool.UserTool;

import java.util.List;



/**
 * 登录界面
 */
public class LoginActivity extends Activity {
    private boolean eye_tv_check = false;
    private Handler handler;
    private String info;
    private List<String> test;
    private EditText edt_username;
    private EditText edt_password;
    private TextView tev_clear;
    private TextView tev_eye;
    private Button btn_login;
    private TextView tv_regiest;




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        loginCheck();

    }

    private void loginCheck() {
        SharedPreferences sharedPreferences = getSharedPreferences("user",MODE_PRIVATE);
        boolean isLogin = sharedPreferences.getBoolean("isLogin",false);
        if(isLogin){
            UserTool.userId = sharedPreferences.getString("id","null");
            UserTool.userPwd = sharedPreferences.getString("pwd","null");
            UserTool.userName = sharedPreferences.getString("name","null");
            startActivity(new Intent(LoginActivity.this,HomeActivity.class));
            finish();
        }else {
            initView();
            initData();
        }
    }

    private void initData() {
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 String name = edt_username.getText().toString();
                String pwd = edt_password.getText().toString();
                if(name==null&&pwd==null){
                    Toast.makeText(LoginActivity.this, "请填写用户信息", Toast.LENGTH_SHORT).show();
                }
                else if(name!=null&&pwd!=null){
                    String params = "userId="+name+"&userPwd="+pwd;
                    Userlogin_Request login_request = new Userlogin_Request(LoginActivity.this,params);
                    login_request.conn(new DoView() {
                        @Override
                        public void setSuccessfulView(Object object) {
                            if(object.toString().equals("true")){
                                //打开本地存储，文件名为“user”
                                SharedPreferences sharedPreferences1 = getSharedPreferences("user",MODE_PRIVATE);
                                SharedPreferences.Editor editor1 = sharedPreferences1.edit();
                                //记录登录状态
                                editor1.putBoolean("isLogin",true);
                                editor1.commit();
                                startActivity(new Intent(LoginActivity.this,HomeActivity.class));
                                finish();
                            }else {
                                Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void setFiledView(Object object) {
                            Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                            Log.e("txh",object.toString());
                        }
                    });
                }
            }
        });
        edt_password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.e("txt",s.toString());
                if(s.length()==0){
                    tev_eye.setVisibility(View.GONE);
                }else {
                    tev_eye.setVisibility(View.VISIBLE);
                }
            }
            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        edt_username.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.e("txt",s.toString());
                if(s.length()==0){
                    tev_clear.setVisibility(View.GONE);
                }else {
                    tev_clear.setVisibility(View.VISIBLE);
                }
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        tev_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                edt_username.setText("");
            }
        });
        tev_eye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(eye_tv_check == false){
                    eye_tv_check = true;
                    tev_eye.setBackgroundResource(R.mipmap.eye_open);
                    edt_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else {
                    eye_tv_check = false;
                    tev_eye.setBackgroundResource(R.mipmap.eye_close);
                    edt_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
        tv_regiest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this,AgrestActivity.class));
            }
        });
    }

    private void initView() {
          edt_username = findViewById(R.id.edt_username);
          edt_password = findViewById(R.id.edt_password);
          tev_clear = findViewById(R.id.tev_clear);
          tev_eye = findViewById(R.id.tev_eye);
          btn_login = findViewById(R.id.btn_login);
          tv_regiest = findViewById(R.id.regiest);
    }


}
